<?php
/**
 * 异常上报应用
 */
namespace Home\Controller;

class ExceptionController extends IndexController
{
    public function index()
    {
        // dump(self::$department);
        $this->assign('user_info', json_encode($_SESSION));
        $this->display();
    }

    /**
     * 获取异常发布数据
     */
    public function get_home()
    {
        if (IS_GET) {
            $data = array(
                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),                array(
                    'department' => '一车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                ),
                array(
                    'department' => '二车间',
                    'content' => '互换件问题，今天到不了',
                    'approve' => '安排到明天完成',
                    'notice' => '二车间，三车间',
                    'time' => date('H:i:s', time())
                )
            );
            $this->ajaxReturn($data);
        }
    }

    public function get_admin()
    {
        if (IS_GET) {
            $data = M('exception')->select();
            foreach ($data as $k => $v) {
                $data[$k]['report_time'] = $v['report_time'] > strtotime('today')
                ? date('H:i:s', $v['report_time'])
                : date('Y-m-d H:i:s', $v['report_time']);
            }
            $this->ajaxReturn($data);
        }
    }

    /**
     * 获取用户个人信息
     */
    public function get_user()
    {
        if (IS_GET) {
            $this->ajaxReturn($_SESSION);
        }
    }

    /**
     * 获取通讯录中的组织架构
     */
    public function getHierarchy()
    {
        dump(self::$we->getUserInfo('zhangjv'));
        dump(self::$we->getUserListInfo(3));
    }

    /**
     * 提交异常
     */
    public function post_report_content()
    {
        if (IS_POST) {
            $post = post();
            $userid = $_SESSION['userid'];

            $data = array();
            $data['report_uid']     = $userid;
            $data['report_content'] = $post['content'];
            $data['report_time']    = time();

            $model = M('exception');

            $res = $model->add($data);

            if ($res) {

                $text = $this->text($userid, '请于两小时内审核异常');

                if ($text['errmsg'] = 'ok') {
                    $task = array();
                    $task['ex_id']  = $res;
                    $task['status'] = 'direct';
                    $task['touser'] = $userid;
                    $task['text']   = '请于两小时内完成审核';
                    task(U('task_status', $task));

                    $this->ajaxReturn(array('callback' => '异常上报成功，已通知相关负责人'));
                }
            } else {
                $this->ajaxReturn(array('callback' => '异常上报失败，请重试'));
            }

        }
    }

    /**
     * 提交异常
     */
    public function exception_commit()
    {
        if (IS_POST) {
            $post = post();
            $data = array(
                'touser' => 'zhangjv',
                'agentid' => 0,
                'msgtype' => 'text',
                'text' => array(
                    'content' => $post['content']
                )
            );

            $json = self::$we->sendMessage($data);

            if ($json['errmsg'] == 'ok') {
                $res = array('callback' => '发送成功', );
                $this->ajaxReturn($res);
            } else {
                $res = array('callback' => '发送失败', );
                $this->ajaxReturn($res);
            }
        }
    }

    /**
     * 审批异常
     */
    public function exception_check()
    {
        if (IS_POST) {
            // $_SESSION['user_info']
        }
    }

    /**
     * 计划任务，定时检查审批状态
     */
    public function task_status()
    {
        // ignore_user_abort(true);
        // set_time_limit(0);

        // if (I('status') == 'direct') {
        //     sleep(10);
        //     $where = array();
        //     $where['ex_id'] = I('ex_id');
        //     $check = M('exception')->where($where)->find();

        //     if ($check['direct_status'] == 1) {
        //         $this->text(I('touser'), '主管看了');
        //     } else {
        //         $this->text(I('touser'), '主管没看');
        //     }

        // } else {
        //     sleep(60);
        // }

    }

}
